<?php
namespace Admin\Controller;
use Think\Controller;
class matchController extends Controller
{
    public function index()
    {
        $this->display();
       
    }

    /*比赛信息*/
    public function matchlist(){
        $match = M('match');
        $matchlist = $match->order("matid desc")->field('matid,matname,matpic,matnumber,mataddress,mattime,matcost')->select();

        for($index=0;$index<count($matchlist);$index++)
        {
            $matchlist[$index]['number']=$index+1;
        }

        $this->assign('matchlist',$matchlist);
        $this->display();
    }
    
    /*比赛修改*/
     public function editmatch()
{
        // $match=M('match');
        // $cid=$_GET['cid'];
        // $res = $match->where("cid=$cid")->select();      
        // $this->matchlist=$res;//name属性对应的值 $this->对应值=$find;
        // $this->display();
        $matid=$_GET['matid'];
        $matchModel=M('match');//表明,conf中定义前缀
        $match = $matchModel->where("matid=$matid")->select();

        //dump($lost);
        $this->match=$match;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }

    public function update(){

            $data=$_POST;
            $file=$_FILES;
            
            $matid=$data['matid'];
                   
            $matchModel = M('match'); // 实例化User模型
            // 要修改的数据对象属性赋值
         
        if ($file['matpic']['size']==0) {
            //没有修改头像
            $result=$matchModel->where("matid=$matid")->save($data);
            
         
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                


                $data['matpic']= $info['matpic']['savename']; 
                $result=$matchModel->where("matid=$matid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/photo/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
           
           if($result){
                $this->success('修改成功！','../match/matchlist');
                }
               else{
                   $this->error('修改失败！');
                   }
    } 

    /*添加比赛*/
     public function addmatch()
    {
     $this->display();
    }

        public function upload(){
        $m = M("match");
        $m->matname=I("post.matname");
        $m->matpic=I("post.matpic");
        $m->matnumber=I("post.matnumber");
        $m->mataddress=I("post.mataddress");
        $m->mattime=I("mattime");
        $m->matcost=I("matcost");
        $m->clubid = $_SESSION['clubid'];
        if(false!=$m->add()){
            $this->redirect('match/matchlist','',2,'添加成功，页面跳转中...');
        }else{
            $this->error("添加失败！");
        }
    }

    /*比赛删除*/
    public function deletematch()
    {
        $matid=I('matid');
        $matchModel = M('match');
        $match=$matchModel->where("matid=$matid")->find();
        // unlink(realpath("./Public/Uploads/logo/".$user["photo"]));
        if($matchModel->delete($vid)) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
    }
    
}